package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.base.server.web.AdminWebContext;
import cn.xy.commons.enums.DisableStatus;
import com.rustic.server.dto.TravelPlanAuditDTO;
import com.rustic.server.enums.AuditStatus;
import com.rustic.server.po.Rustic;
import com.rustic.server.query.TravelPlanAdminListQuery;
import com.rustic.server.dto.TravelPlanAddDTO;
import com.rustic.server.dto.TravelPlanUpdateDTO;
import com.rustic.server.po.TravelPlan;
import com.rustic.server.service.RusticService;
import com.rustic.server.service.TravelPlanService;
import com.rustic.server.vo.TravelPlanAuditResultsVO;
import com.rustic.server.vo.list.TravelPlanAdminListVO;
import com.rustic.server.vo.TravelPlanVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 表[travel_plan]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/travelPlan")
@Api(tags = {"旅游攻略管理接口"})
public class TravelPlanAdminController {

    @Autowired
    private TravelPlanService travelPlanService;

    @Autowired
    private RusticService rusticService;

    @ApiOperation(value = "旅游攻略-列表", notes = "权限key: travel-plan-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('travel-plan-list')")
    public Result<Page<TravelPlanAdminListVO>> list(@Validated TravelPlanAdminListQuery query) {
        LambdaQueryWrapper<TravelPlan> queryWrapper = travelPlanService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<TravelPlan> pageData = travelPlanService.page(query.toPage(), queryWrapper);
        List<TravelPlan> travelPlanList = pageData.getRecords();
        if (CollUtil.isEmpty(travelPlanList)) {
            return Result.data(new Page<>());
        }
        List<TravelPlanAdminListVO> results = travelPlanService.toAdminListVo(travelPlanList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "旅游攻略-详情", notes = "权限key: travel-plan-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "旅游攻略id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('travel-plan-detail')")
    public Result<TravelPlanVO> detail(int id) {
        if (id <= 0) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlan travelPlan = travelPlanService.getById(id);
        if (travelPlan == null) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlanVO travelPlanVO = new TravelPlanVO(travelPlan);
        return Result.data(travelPlanVO);
    }

    @ApiOperation(value = "旅游攻略-增加", notes = "权限key: travel-plan-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('travel-plan-add')")
    @OperateLog(description = "增加旅游攻略")
    public Result<String> add(@RequestBody @Validated TravelPlanAddDTO travelPlanAddDTO) {
        Rustic rustic = rusticService.getById(travelPlanAddDTO.getRusticId());
        if (rustic == null) {
            return Result.error(RusticService.RUSTIC_NOT_EXIST);
        }
        TravelPlan travelPlan = travelPlanAddDTO.toTravelPlan();
        String username = AdminWebContext.getUsername();
        int adminId = AdminWebContext.getAdminId();
        travelPlan.setCreator(username);
        travelPlan.setCreatorId(adminId);
        travelPlan.setStatus(DisableStatus.NORMAL.getValue());
        travelPlan.setAuditId(adminId);
        travelPlan.setAuditor(username);
        travelPlan.setAuditStatus(AuditStatus.PASS.getValue());
        travelPlan.setPublishTime(new Date());
        travelPlan.setAuditTime(new Date());
        travelPlan.setAuditRemark("管理员发布");
        travelPlanService.save(travelPlan);
        return Result.ok("添加旅游攻略成功");
    }

    @ApiOperation(value = "旅游攻略-更新", notes = "权限key: travel-plan-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('travel-plan-update')")
    @OperateLog(description = "更新旅游攻略")
    public Result<String> update(@RequestBody @Validated TravelPlanUpdateDTO travelPlanUpdateDTO) {
        TravelPlan travelPlan = travelPlanService.getById(travelPlanUpdateDTO.getId());
        if (travelPlan == null) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        BeanUtils.copyProperties(travelPlanUpdateDTO, travelPlan);
        travelPlanService.updateById(travelPlan);
        return Result.ok("编辑旅游攻略成功");
    }

    @ApiOperation(value = "旅游攻略-删除", notes = "权限key: travel-plan-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('travel-plan-delete')")
    @OperateLog(description = "删除旅游攻略")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlan travelPlan = travelPlanService.getById(id);
        if (travelPlan == null) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        travelPlanService.removeById(id);
        return Result.ok("删除旅游攻略成功");
    }

    @ApiOperation(value = "旅游攻略-审核", notes = "权限key: travel-plan-audit")
    @PostMapping("/audit")
    @PreAuthorize("@validate.hasPermission('travel-plan-audit')")
    @OperateLog(description = "审核旅游攻略")
    public Result<String> audit(@RequestBody @Validated TravelPlanAuditDTO travelPlanAuditDTO) {
        travelPlanService.audit(travelPlanAuditDTO,AdminWebContext.getAdminId(),AdminWebContext.getUsername());
        return Result.ok("审核提交成功");
    }

    @ApiOperation(value = "旅游攻略-审核结果", notes = "权限key: travel-plan-audit")
    @GetMapping("/auditResults")
    @PreAuthorize("@validate.hasPermission('travel-plan-audit')")
    @OperateLog(description = "查看旅游攻略审核结果")
    public Result<TravelPlanAuditResultsVO> auditResults(int id) {
        if (id <= 0) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlan travelPlan = travelPlanService.getById(id);
        if (travelPlan == null) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlanAuditResultsVO travelPlanAuditResultsVO = new TravelPlanAuditResultsVO(travelPlan);
        return Result.data(travelPlanAuditResultsVO);
    }
}
